__author__ = 'cristian.salamea@gnuthink.com (Cristian Salamea)'

from waveapi import document
from waveapi import events
from waveapi import robot

ROBOT_NAME = 'pytweety'

def StripKnownDomains(p):
  address, domain = p.split('@', 1)
  if domain in GetKnownDomains():
    return address
  else:
    return p
  
def InsertTweetForm(blip, title):
#  blip.SetTitle(title)
  form = blip.GetDocument()
  form.AppendElement(
    document.FormElement(
      document.ELEMENT_TYPE.INPUT,
      'tweet_input',
    ))
  form.AppendElement(
      document.FormElement(
          document.ELEMENT_TYPE.BUTTON,
          'update',
          value='Update!',
          ))
  
def OnParticipantsChanged(properties, context):
  """Invoked when any participants have been added/removed"""
  added = properties['participantsAdded']

def OnRobotAdded(properties, context):
  root_wavelet = context.GetRootWavelet()
  print "VERGAAAAAAAAAAAa", root_wavelet
  if root_wavelet:
    blip = context.GetBlipById(root_wavelet.GetRootBlipId())
  else:
    print "puta madre no hay wave root"
  InsertTweetForm(blip, 'What are you doing ?')

def OnButtonClicked():
  print "clic en update button"
  
if __name__ == '__main__':
  pytweety = robot.Robot(ROBOT_NAME.capitalize(),
                       image_url='http://pytweety.appspot.com/inc/pytweety.png',
                       version = '1',
                       profile_url='http://pytweety.appspot.com')
  pytweety.RegisterHandler(events.WAVELET_PARTICIPANTS_CHANGED, OnParticipantsChanged)
  pytweety.RegisterHandler(events.WAVELET_SELF_ADDED, OnRobotAdded)
  pytweety.RegisterHandler(events.FORM_BUTTON_CLICKED, OnButtonClicked)
  pytweety.Run(debug=True)


